package com.ternnetwork.cms.service.impl.carousel;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.ternnetwork.cms.dao.carousel.CarouselContentDao;
import com.ternnetwork.cms.dao.carousel.CarouselDao;
import com.ternnetwork.cms.model.carousel.Carousel;
import com.ternnetwork.cms.model.carousel.CarouselContent;
import com.ternnetwork.cms.service.carousel.CarouselContentService;
import com.ternnetwork.commons.model.http.BaseResponse;
import com.ternnetwork.commons.utils.ExtendedStringUtils;
import com.ternnetwork.persistence.page.BootstrapGrid;
import com.ternnetwork.persistence.page.Page;


@Service("carouselContentService")
public class CarouselContentServiceImpl implements CarouselContentService {
	
	@Resource
	private CarouselContentDao carouselContentDao;
	
	@Resource
	private CarouselDao carouselDao;
	
	
	public BaseResponse idoAdd(CarouselContent t){
		BaseResponse res=new BaseResponse();
		Carousel carousel=carouselDao.findById(Carousel.class, t.getCarousel().getId());
		if(carousel==null){
			res.setStatus(0);
       		res.setMessage("操作失败，轮播不存在！");
    		return res;
		}		
		t.setCarousel(carousel);
		if(t.getSeqNum()==null||t.getSeqNum()==0){
			long totalCount=carouselContentDao.getTotalCount("select count(id) from CarouselContent where carousel.id=?1", t.getCarousel().getId());
			t.setSeqNum(Integer.parseInt(String.valueOf(totalCount+1)));
		}
		carouselContentDao.persist(t);
		res.setStatus(1);
   		res.setMessage("操作成功！");
		return res;
		
	}
	
	
	
	
	public BaseResponse idoUpdate(CarouselContent t){
		BaseResponse res=new BaseResponse();
		Carousel carousel=carouselDao.findById(Carousel.class, t.getCarousel().getId());
		if(carousel==null){
			res.setStatus(0);
       		res.setMessage("操作失败，轮播不存在！");
    		return res;
		}		
		t.setCarousel(carousel);
		carouselContentDao.saveOrUpdate(t);
		res.setStatus(1);
   		res.setMessage("操作成功！");
		return res;
	}
	
	 public BaseResponse idoDeleteById(long id){
		 BaseResponse res=new BaseResponse();
		 CarouselContent t=carouselContentDao.findById(CarouselContent.class, id);
		 if(t==null){
				res.setStatus(0);
	       		res.setMessage("操作失败，对像不存在！");
	    		return res;
		 }
	     carouselContentDao.delete(t);
	     res.setStatus(1);
	   	 res.setMessage("操作成功！");
		 return res;
	 }
	  public Page<CarouselContent> query(Page page,String carouselId){
			StringBuffer jpql=new StringBuffer("from  CarouselContent t where 1=1");
			List<Object> params=new ArrayList<Object>();
			if(ExtendedStringUtils.isNotEmpty(carouselId)){
				params.add(Long.parseLong(carouselId));
				jpql.append(" and t.carousel.id=?"+params.size());
			}
			    jpql.append(" order by t.seqNum asc ");
			return carouselContentDao.query("select count(t.id) "+jpql.toString(),jpql.toString(), page,params.toArray());
	  }
	  
	  public String queryToJsonStr(Page page,String carouselId){
			Page<CarouselContent> result=query(page, carouselId);
			BootstrapGrid grid=new BootstrapGrid(result);
			return grid.toString();
	  }
	  
	  public List<CarouselContent> findAllByCarouselCode(String carouselCode){
			 return carouselContentDao.findAll("from CarouselContent t where t.carousel.code=?1 order by t.seqNum asc", carouselCode);
	  }

}
